package com.ghostflying.wlanassistant;

import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class WifiConnectByOrderService extends Service {
	private final String TAG = "WifiConnectByOrderService";
	
	private ArrayList<String> list = null;
	private ConnectThread mConnectThread = null;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
    public void onCreate() {
        super.onCreate();
        list = new ArrayList<String>();
        getOrder();
        Log.d(TAG, TAG + "Service Created");
    }
	
	@Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, TAG + "Service Stoped");
    }
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId){
		super.onStartCommand(intent, flags, startId);
		getOrder();
		if (mConnectThread == null){
			mConnectThread = new ConnectThread();
			mConnectThread.start();
		}		
		Log.d(TAG, TAG + "Service Started");

		//stopSelf();
		return 0;
	}
	
	private class ConnectThread extends Thread {
		WifiManager mWifiManager = null;
		List<ScanResult> listResult = null;
		ArrayList<String> listResultSSID = null;
		int position;
		
		public ConnectThread(){
			mWifiManager = (WifiManager) getSystemService(MainActivity.WIFI_SERVICE);
			listResultSSID = new ArrayList<String>();
		}
		
		@Override
		public void run(){
			Log.d(TAG, "ConnectThread Start");
			if (mWifiManager.getConnectionInfo().getSSID().replace("\"", "").equals(list.get(0))){
				Thread.interrupted();
			}
			listResult = mWifiManager.getScanResults();
			for (ScanResult r:listResult){
				if (!listResultSSID.contains(r.SSID)){
					listResultSSID.add(r.SSID);
				}				
			}
			for (position=0;position<list.size();position++){
				if (listResultSSID.contains(list.get(position))){
					connectToAP(position);
					break;
				}
			}
			mConnectThread = null;
		}
		
		private void connectToAP(int p){
			Log.d(TAG, TAG + "Connect to AP");
			for (WifiConfiguration r:mWifiManager.getConfiguredNetworks()){
				if (r.SSID.replace("\"", "").equals(list.get(p))){
					mWifiManager.enableNetwork(r.networkId, true);
					break;
				}
			}
		}
	}
	
	
    private ArrayList<String> getOrder(){    	
    	SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    	list.clear();
    	for (int i = 0; i < settings.getInt("total", 0); i++){
    		list.add(settings.getString(Integer.toString(i), "error"));
/*    		String temp = settings.getString(Integer.toString(i), "error");
    		temp.replace("\"", "");
    		list.add(temp);*/
    	}
    	return list;
    }
	


}
